package com.mck.service.impl;

import com.mck.anno.OperationLog;
import com.mck.entity.Page;
import com.mck.mapper.PageMapper;
import com.mck.service.PageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;

import java.util.List;

/**
 * @version 1.0.0
 * @Author mck
 * @Date 2025/4/13 下午3:42
 * @注释
 */

@Service
public class PageServiceImpl implements PageService {

    @Autowired
    private PageMapper pageMapper;

    // 获取页面列表
    @Override
    @Cacheable(value = "pages", key = "'all'", unless = "#result.isEmpty()")
    public List<Page> getPageList() {
        return pageMapper.getPageList();
    }

    // 获取指定页面列表
    @Override
    @Cacheable(value = "page", key = "#data", unless = "#result.isEmpty()")
    public List<Page> getPageListBySpecific(Page data) {
        return pageMapper.getPageListBySpecific(data);
    }

    // 添加页面
    @OperationLog
    @Override
    @CacheEvict(value = "pages", key = "'all'")
    public void addPage(Page data) {
        data.setCreateTime(LocalDateTime.now());

        pageMapper.addPage(data);
    }

    // 修改页面
    @OperationLog
    @Override
    @Caching(
            evict = {
                    @CacheEvict(value = "pages", key = "'all'"),
                    @CacheEvict(value = "page", key = "#data")
            }
    )
    public void updatePage(Page data) {
        data.setUpdateTime(LocalDateTime.now());

        pageMapper.updatePage(data);
    }

    // 删除页面
    @OperationLog
    @Caching(
            evict = {
                    @CacheEvict(value = "pages", key = "'all'"),
                    @CacheEvict(value = "page", key = "#id")
            }
    )
    public void deletePage(Integer id) {
        pageMapper.deletePage(id);
    }
}
